{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "21cfea06-af98-496a-b13b-106c335a2e65",
   "metadata": {},
   "source": [
    "# Understand Number Spaces - MinMax Mode\n",
    "\n",
    "To enable searches with well known MAX and MIN logic `NumberSpace` creates the opportunity to embed numeric data in a searchable way.\n",
    "\n",
    "Check Similar mode [here](number_embedding_similar.ipynb)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1bf67328-efe5-4c88-9c36-ce2d4b20d89f",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install superlinked==3.38.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "11664035-fff3-4c38-97f3-f2fbb0d46778",
   "metadata": {},
   "outputs": [],
   "source": [
    "from superlinked.framework.common.schema.id_schema_object import IdField\n",
    "from superlinked.framework.common.schema.schema import schema\n",
    "from superlinked.framework.common.schema.schema_object import Integer\n",
    "from superlinked.framework.dsl.index.index import Index\n",
    "from superlinked.framework.dsl.space.number_space import NumberSpace, Mode\n",
    "\n",
    "from superlinked.framework.dsl.executor.in_memory.in_memory_executor import (\n",
    "    InMemoryExecutor,\n",
    ")\n",
    "from superlinked.framework.dsl.source.in_memory_source import InMemorySource\n",
    "from superlinked.framework.dsl.query.query import Query"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5dbad3ef-0e66-4e02-920f-1041eca72787",
   "metadata": {},
   "outputs": [],
   "source": [
    "@schema\n",
    "class Person:\n",
    "    id: IdField\n",
    "    height: Integer  # set height in cms for example\n",
    "    outstanding_debt_amount: Integer  # set in dollars for example\n",
    "\n",
    "\n",
    "person = Person()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e47d6bb-4bf0-4fdb-9e7d-6152272c0060",
   "metadata": {},
   "source": [
    "## Minimum mode\n",
    "\n",
    "For preferring lower numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a0b19b5b-40eb-47aa-9a62-82348048d165",
   "metadata": {},
   "outputs": [],
   "source": [
    "debt_space = NumberSpace(\n",
    "    number=person.outstanding_debt_amount, min_value=0, max_value=1e6, mode=Mode.MINIMUM\n",
    ")  # debts higher than max_value are effectively capped"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3628c066-5491-40ff-867a-f3f7dec6f8ae",
   "metadata": {},
   "source": [
    "## Maximum mode\n",
    "\n",
    "To prefer higher numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2df24eaf-f9b8-404b-8b7f-0a9d2c2284df",
   "metadata": {},
   "outputs": [],
   "source": [
    "height_space = NumberSpace(\n",
    "    number=person.height, min_value=100, max_value=220, mode=Mode.MAXIMUM\n",
    ")  # heights out of this range are considered an outlier in our usecase"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0fa1e273-3ab9-4ffe-97d1-327645c31105",
   "metadata": {},
   "outputs": [],
   "source": [
    "person_index = Index(height_space)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4aed826a-470e-4e53-8f2a-e8e529373b61",
   "metadata": {},
   "outputs": [],
   "source": [
    "source: InMemorySource = InMemorySource(person)\n",
    "executor = InMemoryExecutor(sources=[source], indices=[person_index])\n",
    "app = executor.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "61d76e31-72f4-4404-8cc1-31c08d08d978",
   "metadata": {},
   "outputs": [],
   "source": [
    "source.put(\n",
    "    [\n",
    "        {\n",
    "            \"id\": \"person1\",\n",
    "            \"height\": 175,\n",
    "            \"outstanding_debt_amount\": 0,\n",
    "        },\n",
    "        {\n",
    "            \"id\": \"person2\",\n",
    "            \"height\": 180,\n",
    "            \"outstanding_debt_amount\": 0,\n",
    "        },\n",
    "    ]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "198f0bce-5d7c-412b-a989-d0ab60982770",
   "metadata": {},
   "outputs": [],
   "source": [
    "query = Query(person_index).find(person)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "aaa75750-3b1c-4d68-af9c-133a249f6b0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>height</th>\n",
       "      <th>outstanding_debt_amount</th>\n",
       "      <th>id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>180</td>\n",
       "      <td>0</td>\n",
       "      <td>person2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>175</td>\n",
       "      <td>0</td>\n",
       "      <td>person1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   height  outstanding_debt_amount       id\n",
       "0     180                        0  person2\n",
       "1     175                        0  person1"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = app.query(query)\n",
    "result.to_pandas()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "753c1a9a-2de7-4763-a598-74b9b5910cea",
   "metadata": {},
   "source": [
    "The person having larger height comes first, as it is expected because of the usage of a `NumberSpace` with `MAXIMUM` `Mode`. "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "superlinked-py3.10",
   "language": "python",
   "name": "superlinked-py3.10"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
